/**
 * 
 */
package com.mnurre.shopping.assistant;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * @author michael
 *
 * Custom cursor adapter for displaying the individual categories in a ListView
 */
public class CategoriesCursorAdapter extends SimpleCursorAdapter {
	
	private static final String TAG = "CategoriesCursorAdapter";
	private int mLayout;
	
	public CategoriesCursorAdapter(Context context, int layout, Cursor cursor,
			String[] from, int[] to) {
		super(context, layout, cursor, from, to);
		Log.d(TAG, "Creating CategoriesCursorAdapter.");
		mLayout = layout;
	}

	/* (non-Javadoc)
	 * @see android.widget.SimpleCursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
	 */
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		// super.bindView(view, context, cursor);
		Log.d(TAG, "bindView called.");
		String categoryName = cursor.getString(cursor.getColumnIndex(DataManager.Tables.Categories.COLUMN_NAME));
		int isTaxable = cursor.getInt(cursor.getColumnIndex(DataManager.Tables.Categories.COLUMN_IS_TAXABLE));
		Log.d(TAG, "categoryName is " + categoryName + " and isTaxable is " + String.valueOf(isTaxable));
		TextView categoryNameTextView = (TextView)view.findViewById(R.id.categoryItemNameTextView);
		categoryNameTextView.setText(categoryName);
		TextView categoryIsTaxableTextView = (TextView)view.findViewById(R.id.categoryItemIsTaxableTextView);
		categoryIsTaxableTextView.setText(isTaxable == 0 ? "" : "X");
	}

	/* (non-Javadoc)
	 * @see android.widget.ResourceCursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
	 */
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		// return super.newView(context, cursor, parent);
		Log.d(TAG, "Creating the view for the individual item.");
		final LayoutInflater inflater = LayoutInflater.from(context);
		return inflater.inflate(mLayout, parent, false);
	}

	
}
